package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.SysUserPostMapper;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import com.ruoyi.system.mapper.TMemberMapper;
import com.ruoyi.system.service.ITMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 入会条件信息，存储会员入会条件的各个版本及其状态Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-25
 */
@Service
public class TMemberServiceImpl implements ITMemberService
{
    @Autowired
    private TMemberMapper memberMapper;
    @Autowired
    private SysUserMapper userMapper;
    @Autowired
    private SysUserPostMapper userPostMapper;
    @Autowired
    private SysUserRoleMapper userRoleMapper;

    /*查询根据会员id查询信息*/
    @Override
    public TMember selectTMemberByMemberId(Long memberId) {
        return memberMapper.selectTMemberByMemberId(memberId);
    }

    @Override
    public List<TMember> selectTMemberList(TMember member) {
        /**/

        return memberMapper.selectTMemberList(member);
    }

    @Override
    public int insertTMember(TMember tMember) {
        return memberMapper.insertTMember(tMember);
    }

    @Override
    public int updateTMember(TMember tMember) {
        return memberMapper.updateTMember(tMember);
    }

    @Override
    public int deleteTMemberByMemberIds(Long[] memberIds) {

        //TODO 没有做校验
        /*得到会员id后，根据会员id删除会员数据*/
        return memberMapper.deleteTMemberByMemberIds(memberIds);
    }

    @Override
    public int deleteTMemberByMemberId(Long memberId) {
        return memberMapper.deleteTMemberByMemberId(memberId);
    }

    /*根据会员id查询会员详细信息*/
    @Override
    public MemberDetailDTO selectMemberDetailById(Long memberId) {
        return memberMapper.selectMemberDetailById(memberId);
    }

    /*接收会员申请完成会员表的增加、用户表更新、职位表增加、部门表增加、角色表增加*/
    @Override
    public int insertTMemberInfo(MemberDetailDTO memberDetailDTO) {
        TMember tMember = memberDetailDTO.getMember();
        //增加会员基本信息,同时，用户id也传入了
        Long userId = memberDetailDTO.getUser().getUserId();
        tMember.setUserId(userId);
        int rows=memberMapper.insertTMember(tMember);
        //增加用户基本信息，需要取出用户信息，重新对提交的用户信息进行更新，这里是通过用户id完成对数据的更新，该部分有deptId
        SysUser user = memberDetailDTO.getUser();
        userMapper.updateUser(user);
        //TODO 这里图片路径还没处理
        //增加用户岗位信息
        insertUserPost(user);
        //增加用户角色信息，默认增加的角色是普通角色
        insertUserRole(user);
        return rows;
    }

    @Override
    public List<TMember> selectUserNameByUserId() {
        return memberMapper.selectUserNameByUserId();
    }

    /**
     * 新增用户角色信息
     *
     * @param user 用户对象
     */
    public void insertUserRole(SysUser user)
    {
        this.insertUserRole(user.getUserId(), user.getRoleIds());
    }

    /**
     * 新增用户岗位信息
     *
     * @param user 用户对象
     */
    public void insertUserPost(SysUser user)
    {
        //从用户表中取出封装的post部门个数
        Long[] posts = user.getPostIds();
        if (StringUtils.isNotEmpty(posts))
        {
            // 新增用户与岗位管理
            /*这里是多个岗位，所有用了集合，
            通过岗位集合遍历岗位id，新建一个用户岗位表的实例*/
            List<SysUserPost> list = new ArrayList<SysUserPost>(posts.length);
            for (Long postId : posts)
            {
                SysUserPost up = new SysUserPost();
                /*将用户id和岗位id存储到up对象中，并存储在list集合中*/
                up.setUserId(user.getUserId());
                up.setPostId(postId);
                list.add(up);
            }
            /*通过方法完成多个数据的插入*/
            userPostMapper.batchUserPost(list);
        }
        //注意数据库的变动

    }
    /**
     * 新增用户角色信息
     *
     * @param userId 用户ID
     * @param roleIds 角色组
     */
    public void insertUserRole(Long userId, Long[] roleIds)
    {
        //获取用户id和角色组
        if (StringUtils.isNotEmpty(roleIds))
        {
            // 新增用户与角色管理
            List<SysUserRole> list = new ArrayList<SysUserRole>(roleIds.length);
            for (Long roleId : roleIds)
            {
                SysUserRole ur = new SysUserRole();
                ur.setUserId(userId);
                ur.setRoleId(roleId);
                list.add(ur);
            }

            userRoleMapper.batchUserRole(list);
        }
        //在会员申请的时候不能自己选择权限，此处将权限默认设置为101
        Long roleId=101L;
        SysUserRole userRole = new SysUserRole();
        userRole.setUserId(userId);
        userRole.setRoleId(roleId);
        userRoleMapper.inserCommonRole(userRole);
    }

}
